import { createArrayByLen } from "@/utils/array";
import { Date } from "./date";

export const createCalendar = (
  month: string,
  format: (day: { text: number; date: string }, index: number) => any
) => {
  const date = Date.getDate(month);
  const firstDate = date.date(1);

  const lastDateList = createArrayByLen(date.date(1).day())
    .map((i, d) => {
      const tempDate = firstDate.subtract(d + 1, "day");
      return { text: tempDate.date(), date: tempDate.format(Date.ftDate) };
    })
    .sort((a, b) => a.text - b.text);

  const dateList = createArrayByLen(date.daysInMonth()).map((i, d) => {
    const text = d + 1;
    return { text, date: `${month}-${(text + "").padStart(2, "0")}` };
  });

  const lastDate = date.date(date.daysInMonth());
  const nextMonth = lastDate.add(1, "day").format(Date.ftMonth);
  const nextDateList = createArrayByLen(6 - lastDate.day()).map((i, d) => {
    const text = d + 1;
    return { text, date: `${nextMonth}-${(text + "").padStart(2, "0")}` };
  });

  const list = lastDateList.concat(dateList).concat(nextDateList);
  return format ? list.map((i, d) => format(i, d)) : list;
};
